package com.rk.app.poll.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.rk.app.poll.client.viewer.PollViewerFactory;

public class ViewResult implements EntryPoint {
	private VerticalPanel mainPanel = new VerticalPanel();

	public static final PollServiceAsync pollingService = GWT.create(PollService.class);

	private Poll poll;

	int row = -1;

	public void onModuleLoad() {
		String pollId = Window.Location.getParameter("id");
		if (pollId == null || pollId.trim().equals("")) {
			Window.alert("Provide poll id to view poll results.");
			Window.Location.replace(Window.Location.getProtocol() + "/main.jsp");
			return;
		}
		pollingService.getPoll(pollId, new AsyncCallback<Poll>() {

			public void onFailure(Throwable caught) {
			}

			public void onSuccess(Poll result) {
				poll = result;
				initPR();
			}
		});

	}

	private void initPR() {
		mainPanel.setWidth("100%");
		Widget viewer = PollViewerFactory.createPollResultViewer(poll);
		mainPanel.add(viewer);
		RootPanel.get("polls").add(mainPanel);

	}

}
